import java.util.*;

public class ArrayDemo {

	protected static void arrayLoop() {
		String month[] = {
			"January",
			"February",
			"March",
			"April",
			"May",
			"June",
			"July",
			"August",
			"September",
			"October",
			"November",
			"December",
		};

		// 基础for 循环遍历
		for(int i=0; i<month.length; ++i){
			System.out.println("basic for " + i + ", " + month[i]);
		}

		// 高级for 循环遍历
		int index=0;
		for(String str : month) {
			System.out.println("advance for " + index + ", " + str);
			++index;
		}

		// 转换为容器实例后，使用迭代器循环遍历
		List<String> monthArr = Arrays.asList(month);
		Iterator iter = monthArr.iterator();
		index=0;
		while (iter.hasNext()) {
			System.out.println("iterator " + index + ", " + iter.next());
			++index;
		}
	}

	protected static void arraySort() {
		ArrayList<String> monthArr = new ArrayList<String>(Arrays.asList(
				"January",
				"February",
				"March",
				"April",
				"May",
				"June",
				"July",
				"August",
				"September",
				"October",
				"November",
				"December"));

		// Collections 提供的sort
		ArrayList<String> monthArrSorted = new ArrayList<String>(monthArr);
		Collections.sort(monthArrSorted);
		int index=0;
		for(String str : monthArrSorted) {
			System.out.println("sorted " + index + ", " + str);
			++index;
		}

		// 按字符串第二个sort whith Comparator
		ArrayList<String> monthArrSorted2 = new ArrayList<String>(monthArr);
		Collections.sort(monthArrSorted2, new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				// 返回1 表示 o1 按升序 排在 o2 后面
				return o1.charAt(1) > o2.charAt(1) ? 1 : -1;
			}
		});
		index=0;
		for(String str : monthArrSorted2) {
			System.out.println("sorted by charAt(1) " + index + ", " + str);
			++index;
		}
	}

	protected static void demoLogic() {
		DateTimeDemo.logTs("arrayLoop");
		arrayLoop();

		DateTimeDemo.logTs("arraySort");
		arraySort();
	}

}
